// Created by WXX on 2021/9/24 11:15
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

/**
 * 执行用时：8 ms, 在所有 C++ 提交中击败了74.24%的用户
 * 内存消耗：8.9 MB, 在所有 C++ 提交中击败了8.58%的用户
 */
class Solution {
public:
    string licenseKeyFormatting(string s, int k) {
        string t;
        for (char c : s)
            if (c != '-')
                t += toupper(c);

        if (k >= t.size()) return t;

        int cnt = t.size() / k;
        string res = t.substr(0, t.size() - k * cnt);
        for (int i = 0, start = t.size() - k * cnt; i < cnt; i++) {
            if (start) res += '-';
            res += t.substr(start, k);
            start += k;
        }
        return res;
    }
};

int main() {

    cout << Solution().licenseKeyFormatting("5F3Z-2e-9-w", 4) << endl;
    cout << Solution().licenseKeyFormatting("2-5g-3-J", 2) << endl;

    return 0;
}
